// pages/packet3/packet3.js 
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logo: '/img/banner.jpg',
    num:0
  },

  /* ----------------------更改logo---------------------- */
  logoHandle: function () {
    let that = this
    let logo = that.data.logo
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        that.setData({
          logo: tempFilePaths
        })
      }
    })
  },
  numHandle:function(e){
    this.setData({
      num:e.detail.value
    })
  },
  msginput: function (e) {
    var that = this
    if (e.detail.value) {
      that.setData({
        hbtext: e.detail.value
      })
    } else {
      that.setData({
        hbtext: ''
      })
    }
  },
  makecard: function () {
    var that = this
    var num = parseInt(that.data.num)
    var pigcount = that.data.info.pigcount
    
    var hbtext = that.data.hbtext
    if (!hbtext) {
      hbtext = '恭喜发财，大吉大利'
    }
    var data = wx.getStorageSync('requestdata');

    data.logo = that.data.info.comlogo
    data.name = that.data.info.comname
    data.hbtext = hbtext
    data.hbtype = 2
    if (num > pigcount || num<0) {
      wx.showToast({
        title: '金猪卡数不足',
        icon: "none"
      })
      return;
    } else {
      data.pignum = num
    }
    // console.log(data); return;
    wx.request({
      url: app.siteinfo.rootpath + 'createhb',
      data: data,
      success: function (res) {
        if (res.data.code == 200) {
            wx.removeStorageSync('music')
            wx.removeStorageSync('greet')
            wx.removeStorageSync('requestdata')
            wx.redirectTo({
              url: '/pages/preview/preview?id=' + res.data.id,
            })
        } else {
          wx.showToast({
            title: '贺卡生成失败',
            icon: "none"
          })
        }
      }
    })
    // console.log(data)
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.siteinfo.rootpath +'getcominfo',
      data:{id:app.siteinfo.userinfo.id},
      success:function(res){
        that.setData({
          info:res.data.info
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})